import PySimpleGUI as sg
import winreg


def read_registry_value(key_path, value_name):
    """读取注册表值"""
    try:
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path)# 打开注册表
        return winreg.QueryValueEx(key, value_name)[0]# 查询注册表的值
    except WindowsError:
        return "Not found"


layout = [
    [sg.Text("注册表值",text_color='blue')],
    [sg.Text("当前构建版本"), sg.Text(size=(30, 1), key="_CURRENT_BUILD_")],
    [sg.Text("显示版本"), sg.Text(size=(30, 1), key="_DISPLAY_VERSION_")],
    [sg.Text("路径名称"), sg.Text(size=(30, 1), key="_PATH_NAME_")],
    [sg.Text("产品名称"), sg.Text(size=(30, 1), key="_PRODUCT_NAME_")],
    [sg.Button("查询")]
]

window = sg.Window("Windows Registry Reader", layout)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    if event == "查询":
        current_build = read_registry_value(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuild")
        display_version = read_registry_value(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "DisplayVersion")
        path_name = read_registry_value(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "PathName")
        product_name = read_registry_value(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName")

        window["_CURRENT_BUILD_"].update(current_build)
        window["_DISPLAY_VERSION_"].update(display_version)
        window["_PATH_NAME_"].update(path_name)
        window["_PRODUCT_NAME_"].update(product_name)

window.close()
